angular.module('Controller')
.directive('qgyOfflineCheckIn',[()->
    return {
        restrict:'E'
        replace:true
        templateUrl:'templates/business-manage/check-in/offline-check-in.html'
        controller:'QgyOfflineCheckInCtrl'
    }
])

.controller 'QgyOfflineCheckInCtrl',(urls,$scope,wfAlert,commonConstant,$http,$localStorage)->
    $scope.contractNew={
        apartmentId:undefined
        buildingId:undefined
        floor:undefined
        houseId:undefined
        payWay:undefined
        lease:undefined
        leaseStart:undefined
        created:false
    }
    $scope.person={
        customerName:undefined
        customerPhone:undefined
        idCard:undefined
        idcardFront:undefined
        idcardBack:undefined
        sex:0
    }
    $scope.pay={
        payMoney:undefined
        payType:undefined
        moneyDesc:undefined
    }
    $('#off-idcard-front').change(()->
        formData = new FormData()
        formData.append('file', $('#off-idcard-front')[0].files[0])
        $http({
            url:urls.backend + "/image?token=" + $localStorage['token'],
            method:'POST'
            headers:{
                'Content-Type': undefined
            }
            data:formData
            transformRequest: angular.identity
        }).success((resp)->
            if resp.code==0
                saveUrl = resp.message.saveUrl+""
                if(saveUrl.indexOf("http")=='0')
                    $scope.person.idcardFront = saveUrl
                else
                    $scope.person.idcardFront=urls.backend+'/public/'+saveUrl
        )
    )
    $('#off-idcard-back').change(()->
        formData = new FormData()
        formData.append('file', $('#off-idcard-back')[0].files[0])
        $http({
            url:urls.backend + "/image?token=" + $localStorage['token'],
            method:'POST'
            headers:{
                'Content-Type': undefined
            }
            data:formData
            transformRequest: angular.identity
        }).success((resp)->
            if resp.code==0
                saveUrl = resp.message.saveUrl+""
                if(saveUrl.indexOf("http")=='0')
                    $scope.person.idcardBack = saveUrl
                else
                    $scope.person.idcardBack=urls.backend+'/public/'+saveUrl
        )
    )
    $scope.cotenant=[]
    $scope.apartmentList=[]
    $scope.buildingList=[{id:undefined,name:'未选择'}]
    $scope.floorList=[{id:undefined,name:'未选择'}]
    $scope.houseList=[{id:undefined,houseNo:'未选择'}]
    $scope.payWayList=[{value:undefined,name:'--'}]
    $scope.leaseList=[{value:undefined,name:'--'}]
    $scope.offPayTypeList=commonConstant.offPayTypeList()
    loadApartment=()->
        $scope.apartmentList=[]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
                if $scope.apartmentList.length>0
                    $scope.contractNew.apartmentId=$scope.apartmentList[0].id
                    $scope.loadBuilding()
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    _.extend $scope,
        resetHouse:()->
            $scope.contractNew.houseId=undefined
            $scope.houseInfo=undefined
            $scope.houseList=[{id:undefined,houseNo:'未选择'}]
            $scope.payWayList=[{value:undefined,name:'--'}]
            $scope.leaseList=[{value:undefined,name:'--'}]
            $scope.contractNew.payType=undefined
        resetBuilding:()->
            $scope.contractNew.buildingId=undefined
            $scope.resetFloor()
        resetFloor:()->
            $scope.contractNew.floor=undefined
            $scope.floorList=[{id:undefined,name:'未选择'}]
            $scope.resetHouse()
        loadBuilding:()->
            $scope.buildingList=[{id:undefined,name:'未选择'}]
            reqData={page: 0,size: 999,apartmentId:$scope.contractNew.apartmentId,sort:'id,ASC'}
            $http.get('/building',params:reqData)
            .success (resp)->
                if resp.code==0
                    $scope.buildingList=$scope.buildingList.concat(resp.message.content)
                    $scope.resetBuilding()
        loadFloor:()->
            if $scope.contractNew.buildingId==undefined
                $scope.resetFloor()
            else
                $scope.resetFloor()
                building=_.find($scope.buildingList,(theBd)->theBd.id==$scope.contractNew.buildingId)
                $scope.floorList=$scope.floorList.concat(building.floorDesc) if building
        loadHouse:()->
            if $scope.contractNew.floor==undefined
                $scope.resetHouse()
                return
            $scope.houseList=[{id:undefined,houseNo:'未选择'}]
            reqData={
                page: 0,size: 999,
                apartmentId:$scope.contractNew.apartmentId
                buildingId:$scope.contractNew.buildingId
                floor:$scope.contractNew.floor
                status:1
            }
            $http.get('/house',params:reqData)
            .success (resp)->
                if resp.code==0
                    $scope.resetHouse()
                    $scope.houseList=$scope.houseList.concat(resp.message.content)
        pickHouse:()->
            if $scope.contractNew.houseId==undefined
                $scope.houseInfo==undefined
                $scope.contractNew.payType=undefined
                $scope.payWayList=[{value:undefined,name:'--'}]
                $scope.leaseList=[{value:undefined,name:'--'}]
            else 
                $scope.houseInfo=_.find($scope.houseList,(theRoom)->theRoom.id==$scope.contractNew.houseId)
                console.log('houseInfo',$scope.houseInfo)
                $scope.payWayList=commonConstant.transferpayWayList($scope.houseInfo.payType)
                $scope.leaseList=commonConstant.leaseList()
                $scope.contractNew.lease=$scope.leaseList[1].value
                if $scope.payWayList.length>0
                    $scope.contractNew.payWay=$scope.payWayList[0].value
        uploadIDCFront:()->
            if !$scope.contractNew.created
                $('input[id=off-idcard-front]').click();
            ''
        uploadIDCBack:()->
            if !$scope.contractNew.created
                $('input[id=off-idcard-back]').click();
            ''
        offlineCheckIn:()->
            ''
        offlineCotenant:()->
            if $scope.contractNew.created
                $scope.openCotenant('view',$scope.cotenant)
            else
                $scope.openCotenant('edit',$scope.cotenant)
        createContract:()->
            if $scope.contractNew.payWay==1 && $scope.contractNew.lease==1
                wfAlert.toastError('付款方式为押一付三时不允许租期少于三个月')
                return
            if $scope.cotenant.length==0
                wfAlert.toastError('至少包含一个住客的信息')
                return
            if !$scope.person.idcardFront || !$scope.person.idcardBack
                wfAlert.toastError('请上传身份证正反面照片')
                return
            $scope.contractNew.creating=true
            data={
                "houseId":$scope.contractNew.houseId,"payWay":$scope.contractNew.payWay,
                "lease":$scope.contractNew.lease,
                "leaseStart":moment($scope.contractNew.leaseStart).format('YYYY-MM-DD'),
                "customerCard":$scope.person.customerCard,"customerName":$scope.person.customerName
                "customerPhone":$scope.person.customerPhone,"sex":$scope.person.sex
                "idcardFront":$scope.person.idcardFront,"idcardBack":$scope.person.idcardBack
#                "payMoney":7200.000,
                "cotenant":$scope.cotenant
            }
            $http.post('/contract',data).success((resp)->
                $scope.contractNew.creating=false
                if resp.code==0
                    wfAlert.toastInfo('合同生成成功')
                    $scope.contractNew.created=true
                    loadContract(resp.message.contractId)
                    console.log('contractData',$scope.contractData)
                else
                    wfAlert.toastError(resp.message)
            )
        loadContract:(id)->
            return if id==undefined
            $http.get("/contract/#{id}/detail")
            .success((resp)->
                if resp.code==0
                    $scope.contractData=resp.message
                    $scope.pay.payType=3
            )
        confirmPay:()->
            billId=$scope.contractData.billId
            $http.put("/bill/#{billId}/pay")
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('确认付款成功')
                    $scope.contractData?.status=2
                else
                    wfAlert.toastError(resp.message)
            )
        offlineCheckIn:()->
            $http.post("/contract/checkIn/#{$scope.contractData.id}")
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('入住成功')
            )